import requests

url = 'https://api.houzi8.com/weapi/audio/list'

headers = {
    "accept": "application/json, text/plain, */*",
    "accept-language": "zh-CN,zh;q=0.9",
    "cache-control": "no-cache",
    "origin": "https://houzi8.com",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://houzi8.com/peiyue.html?usid=24694",
    "sec-ch-ua-mobile": "?0",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
}

cookies = {
    "track_id": "16474a655d7ea30215f176003b6e29d9ad33480b6e02c1cd190e5c7c74db11aca%3A2%3A%7Bi%3A0%3Bs%3A8%3A%22track_id%22%3Bi%3A1%3Bs%3A55%3A%225d36e573699be5f2095e55324b38ea9f66fd1bc4030f49.04028476%22%3B%7D",
    "user_source": "d26dc8e4ee80586e031f077dbf5fda3e7a70cc513929e8ade015f6c4a529f5c3a%3A2%3A%7Bi%3A0%3Bs%3A11%3A%22user_source%22%3Bi%3A1%3Bs%3A5%3A%2224694%22%3B%7D",
    "user_source_permanent": "8a7508f2cded707c6eb80060356d9382e35d61bc40afd078c56fc958551ef7dfa%3A2%3A%7Bi%3A0%3Bs%3A21%3A%22user_source_permanent%22%3Bi%3A1%3Bs%3A5%3A%2224694%22%3B%7D",
    "todayViewMark": "869ad589fae6c88a6771e92dcd4c6244ecb5a7f270c1b784ad38801ba6646923a%3A2%3A%7Bi%3A0%3Bs%3A13%3A%22todayViewMark%22%3Bi%3A1%3Bi%3A1%3B%7D",
    "__root_domain_v": ".houzi8.com",
    "_qddaz": "QD.751027863763319",
    "_c_WBKFRo": "B7g0wZ1dvbfSHZjXuHNK70Ys5aUBkr4Fe78F7b5G",
    "_search_kw": "81d9dbe5408c199f8f7b700576caf8cd37d0063bf1145c8becd03dc71f6c0379a%3A2%3A%7Bi%3A0%3Bs%3A10%3A%22_search_kw%22%3Bi%3A1%3Bs%3A0%3A%22%22%3B%7D",
    "Hm_lvt_bb1a215c22b753f9361786dbc9433727": "1727863749,1727867669",
    "HMACCOUNT": "2D50FC184ABBABEF",
    "YPSSESSION": "up4i5p79uj8e0ki0r3q4catnff",
    "_identity": "1df182db1313dfeb3d88cf0558a7278d26cd27940e170587f270ded4bc84ca6aa%3A2%3A%7Bi%3A0%3Bs%3A9%3A%22_identity%22%3Bi%3A1%3Bs%3A52%3A%22%5B1614362%2C%22FeZqK2waa1ZK80diavsmNCOKmk88FPKl%22%2C2592000%5D%22%3B%7D",
    "_csrf": "25ab3743c60b1abb05e912c6050d6644ce50f9cd2ed0100aaf0ec694af0255dfa%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22l57UGM0vAWZefm4w4jwhu5Hj2-Q4y7oY%22%3B%7D",
    "hunl_a8379dd867dbdb2a5569770adb5f9419": "924ada80c829766c07ad82a8bf28cf5f06abc4df2f6171ab63ddac70331c0f69a%3A2%3A%7Bi%3A0%3Bs%3A37%3A%22hunl_a8379dd867dbdb2a5569770adb5f9419%22%3Bi%3A1%3Bs%3A104%3A%22YToyOntzOjU6InZhbHVlIjtzOjEzOiI2NmZkMmIxZTlhNDI5IjtzOjQ6InRpbWUiO3M6MTk6IjIwMjQtMTAtMDIgMTk6MTQ6MzgiO30%3D%22%3B%7D",
    "Hm_lpvt_bb1a215c22b753f9361786dbc9433727": "1727867763"
}

params = {
    "type": "1",
    "sort": "1",
    "page": "1",
    "page_size": "120", # 一页的数据量 这里我修改为120 发现是可以获取到的 最大能是多少需要测试
    "country": "zh"
}

# 发送GET请求
response = requests.get(url, headers=headers, cookies=cookies, params=params)

# 以上代码是curl转换的

path = 'D:/webDownload/spiderData/hzyy_mp3/' # 本地磁盘路径
for i in response.json()['data']['audio_list']:
    with open(path + i['title'] + '.mp3', 'wb') as f:
        f.write(requests.get(i['preview_url'], headers=headers, cookies=cookies, ).content)
        print(i['title'] + '下载成功')
